using System.Net;
using System.Net.Sockets;

namespace ZerAx.Net
{
    public class TcpConnector : IConnector
    {
        public readonly Socket listener;
        public TcpConnector(IPEndPoint endPoint)
        {
            listener = new(SocketType.Stream, ProtocolType.Tcp)
            {
                Blocking = false
            };
            listener.Bind(endPoint);
        }

        public void Init()
        {
            listener.Listen(100);
        }

        public bool TryProduceConnection(out IConnection connection)
        {
            connection = null;
            if (listener.Poll(0, SelectMode.SelectRead))
            {
                connection = new TcpConnection(listener.Accept());
                return true;
            }
            return false;
        }

        public void Close()
        {
            listener.Close();
        }
    }
}